import requests
import sqlite3
import json
import time

# 连接到数据库（如果不存在，则会创建）
conn = sqlite3.connect('./data/pl5.db')
# 创建游标对象，用于执行SQL语句
cursor = conn.cursor()

url = 'https://webapi.sporttery.cn/gateway/lottery/getHistoryPageListV1.qry?gameNo=350133&provinceId=0&pageSize=30&isVerify=1&pageNo={}'

# 爬取10页的内容
for i in range(11, 21):
    pl5_data = requests.get(url.format(i))
    if pl5_data.status_code != 200:
        print("获取失败,请勿频繁请求")
    data = pl5_data.json()
    # print(data.keys())
    data_list = data['value']['list']

    inst_sql = '''
        insert into pl5 values (?,?,?,?,?,?,?)
    '''

    for d in data_list:
        lotteryDrawNum = d['lotteryDrawNum']
        datetime = d['lotteryDrawTime']
        res = d['lotteryDrawResult'].split(" ")
        cursor.execute(inst_sql, tuple([lotteryDrawNum, datetime] + res))
    conn.commit()
    time.sleep(0.5)
